﻿#include "EditorComponentPropertyView.h"

#include "Widgets/TextWidget.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/Positioners/VerticalPositionerWidget.h"
#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/EditorCheckBoxWidget.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Property/ReflectedProperty/EditorReflectedPropertyView.h"
#include "Property/ReflectedProperty/EditorReflectedPropertyViewModel.h"

#include "Asset/EditorDefaultPackage.h"


#include "Window/EditorWindowGroupViewModel.h"

void REditorComponentPropertyView::BindViewModel(TSharedObjectPtr<REditorPropertyViewModel> InViewModel)
{
    Super::BindViewModel(InViewModel);
    ComponentViewModel =  InViewModel;

    Properties=NewObject<REditorReflectedPropertyView>(this);
    Properties->BindViewModel(ComponentViewModel->GetProperties());


}
void REditorComponentPropertyView::CreateWidget(TSharedObjectPtr<RVerticalPositionerWidget> InPropertyList)
{
    auto Root=NewWidget<RVerticalPositionerWidget>(InPropertyList);
    Root->Anchor.HorizontalFill(InPropertyList);
    Root->Height=ComponentViewModel->LineHeight;
    RWidgetWeakPtr RootWeak=Root;
   
    auto LabelLine=NewWidget<RWidget>(Root);
    LabelLine->Height=ComponentViewModel->LineHeight;
    LabelLine->Anchor.HorizontalFill(InPropertyList);


    auto ComponentLabelParent=NewWidget<RMouseWidget>(LabelLine);
    ComponentLabelParent->OnClicked.BindLambda([this](const SMouseButtonEvent&){
        ComponentViewModel->ToggleExpand();
        return true;
    });
    ComponentLabelParent->Anchor.RightReference=LabelLine;
    ComponentLabelParent->Anchor.RightOffset=ComponentViewModel->LineHeight; //for button 
    ComponentLabelParent->Height=ComponentViewModel->LineHeight;

    auto ComponentLabel=NewWidget<RHorizontalPositionerWidget>(ComponentLabelParent);
    ComponentLabel->Anchor.Fill(ComponentLabelParent);
    ComponentLabel->Spacing=5;


    auto ExpandButton=NewWidget<RImageWidget>(ComponentLabel);
    ExpandButton->Width=ComponentViewModel->LineHeight;
    ExpandButton->Height=ComponentViewModel->LineHeight;
    ExpandButton->Image=[this](){
        return ComponentViewModel->GetExpandIcon();
    };

    auto AutoBeginPlay=NewWidget<REditorCheckBoxWidget>(ComponentLabel);
    AutoBeginPlay->Width=ComponentViewModel->LineHeight;
    AutoBeginPlay->Height=ComponentViewModel->LineHeight;
    AutoBeginPlay->OnCheckStateChanged.Bind([this](ECheckBoxWidgetState){
        ComponentViewModel->ToggleAutoBeginPlay();
    });
    AutoBeginPlay->CheckState=[this](){
        return ComponentViewModel->GetAutoBeginPlayState();
    };

    auto NameLabel=NewWidget<RTextWidget>(ComponentLabel);
    NameLabel->Text=[this](){
        return ComponentViewModel->GetName();
    };
    NameLabel->Anchor.VerticalFill(ComponentLabel);
    NameLabel->VerticalAlignment=ETextVerticalAlignment::Center;

    //more operations
    auto MoreButton=NewWidget<RRectangleWidget>(LabelLine);
    MoreButton->Width=ComponentViewModel->LineHeight;
    MoreButton->Height=ComponentViewModel->LineHeight;
    MoreButton->Anchor.VerticalCenterReference=LabelLine;
    MoreButton->Anchor.RightReference=LabelLine;
    MoreButton->Anchor.RightOffset=2;
    MoreButton->Radius=4;
    MoreButton->FillColor=REditor::Get()->CurrentTheme->ImageButtonHovorColor;

    auto MoreButtonImage=NewWidget<RImageWidget>(MoreButton);
    MoreButtonImage->Width=ComponentViewModel->LineHeight;
    MoreButtonImage->Height=ComponentViewModel->LineHeight;
    MoreButtonImage->Image= REditorDefaultPackage::Get()->
        GetIcon(U"CodiconEllipsis");
    MoreButtonImage->Anchor.CenterIn(MoreButton);

    auto MoreButtonMouse=NewWidget<RMouseWidget>(MoreButton);
    MoreButtonMouse->Anchor.Fill(MoreButton);
    RMouseWidgetWeakPtr MoreButtonMouseWeak=MoreButtonMouse;
    MoreButton->Fill=[MoreButtonMouseWeak](){
        return MoreButtonMouseWeak->IsHovered();
    };
    MoreButtonMouse->OnClicked.BindLambda([this,MoreButtonMouseWeak](const SMouseButtonEvent& InEvent){
        //create a context menu
        TVector<SEditorContextMenuLine> RootContextMenu;

        //add Remove component
        {
            auto& Remove= RootContextMenu.Emplace();
            Remove.Text=U"移除组件";
            Remove.OnClicked.Bind([this](){
                ComponentViewModel->RemoveComponent();
            });
        }
       
        REditorWindowGroupViewModel::Get()->ShowContextMenu(
        RootContextMenu
        ,MoreButtonMouseWeak
        ,150
        );

        return true;

    });

    //properties
    auto PropertyWidgetParent=NewWidget<RVerticalPositionerWidget>(InPropertyList);
    PropertyWidgetParent->Visible=[this]()
    {
        return ComponentViewModel->IsExpand();
    };
    PropertyWidgetParent->Anchor.HorizontalFill(InPropertyList);
    RVerticalPositionerWidgetWeakPtr WeakPropertyWidgetParent=PropertyWidgetParent;
    PropertyWidgetParent->Height=[this,WeakPropertyWidgetParent]()->float
    {
        if(!ComponentViewModel->IsExpand())
        {
            return 0; 
        }

        return WeakPropertyWidgetParent->CalculateContentHeight();
    };
    PropertyWidgetParent->Spacing=0;
    Properties->CreateWidget(PropertyWidgetParent);
    

}
